package com.cet.qz.unit01.chapter1_1;

import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdRandom;

/**
 * @program: algorithm
 * @description: 糟糕的打乱
 * @author: 陈恩涛
 * @create: 2020-12-23 21:54
 **/
public class Exec03 {

    /*
     *方法描述： 随机将double数组中的元素排序
     *
     * @param a
     * @return void
     * @author chenentao
     * @date 2021/1/6 22:13
     */
    public static void shuffle(int[] a) {
        int N = a.length;
        for (int i = 0; i < N; i++) {
            //将a[i]和a[0..N-1]中任意一个元素交换
            int r = StdRandom.uniform(N);
            int temp = a[i];
            a[i] = a[r];
            a[r] = temp;
        }
    }

    /*
     *方法描述： 初始化一个int数组，a[i]=i
     *
     * @param n
     * @return int[]
     * @author chenentao
     * @date 2021/1/7 7:53
     */
    public static void initArr(int[] a){
        for (int i = 0; i < a.length; i++) {
            a[i] = i;
        }
    }

    public static int[][] shuffleTest(int M, int N){
        int[][] result = new int[M][M];
        int[] arr = new int[M];

        for (int i = 0; i < N; i++) {
            initArr(arr);
            shuffle(arr);
            for (int j = 0; j < arr.length; j++) {
                result[j][arr[j]]++;
            }
        }

        return result;
    }

    public static void main(String[] args) {
        System.out.println("请输入数组长度M：");
        int M = StdIn.readInt();
        System.out.println("请输入打乱次数N：");
        int N = StdIn.readInt();
        int[][] result = shuffleTest(M, N);
        for (int i = 0; i < result.length; i++) {
            for (int j = 0; j < result[0].length; j++) {
                System.out.print(result[i][j]+"\t");
            }
            System.out.print("\n");
        }
    }
}
